package Module;

import com.mysql.jdbc.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-12-20
 * Time: 20:05
 * To BE A Better Person-2020-3-2
 */
public class BlogDao {
    public void addBlog(Blog blog) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;

        try {
            connection=DButil.getConnection();
            String sql="insert into blog values (null,?,?,?,now());";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,blog.getTitle());
            preparedStatement.setString(2, blog.getContent());
            preparedStatement.setInt(3,blog.getUserid());
            int ret=preparedStatement.executeUpdate();
            if(ret == 1){
                System.out.println("插入成功！");
            }else{
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

            DButil.close(connection,preparedStatement,null);
        }
    }

    //这个是用户页面显示来使用的
    public ArrayList<Blog> searchAll() throws SQLException {
        Connection connection=null;
        ResultSet resultSet=null;
        PreparedStatement statement=null;
        ArrayList<Blog> list=new ArrayList<>();
        try{
            connection = DButil.getConnection();
            String sql="select * from blog order by posttime desc;";
            statement=connection.prepareStatement(sql);

            resultSet=statement.executeQuery(sql);
            while(resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogid(resultSet.getInt("blogid"));
                blog.setTitle(resultSet.getString("title"));
                blog.setUserid(resultSet.getInt("userid"));
                //就这里啊，你吧字段名改的和前端一样
                blog.setPosttime(resultSet.getTimestamp("posttime"));
                String temp=resultSet.getString("content");
                blog.setContent(temp.substring(0,temp.length()/2));
                list.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(connection,statement,resultSet);
        }
        return list;
    }

    public Blog search(int blogid) {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try{
            connection=DButil.getConnection();
            String sql="select * from blog where blogid = ?;";
            statement= connection.prepareStatement(sql);
            statement.setInt(1,blogid);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                Blog blog1=new Blog();
                blog1.setBlogid(resultSet.getInt("blogid"));
                blog1.setTitle(resultSet.getString("title"));
                blog1.setUserid(resultSet.getInt("userid"));
                blog1.setPosttime(resultSet.getTimestamp("posttime"));
                String temp=resultSet.getString("content");
                blog1.setContent(temp);
                return  blog1;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            assert connection != null;
            assert statement != null;
            assert resultSet != null;
            DButil.close(connection,statement,resultSet);
        }
        return null;
    }

    public void delete(int blogid) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try{
            connection = DButil.getConnection();
            String sql="delete from blog where blogid = ?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogid);
            statement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            assert connection != null;
            assert statement != null;
            DButil.close(connection,statement,null);
        }
    }
}
